package com.bolt.report.engine.pool;

import com.alibaba.druid.pool.DruidDataSource;
import com.bolt.report.definition.JdbcDataSourceDefinition;
import com.bolt.report.exception.ReportException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DruidJdbcPoolImpl implements JdbcConnectionPool {

    private static Map<String, DruidDataSource> map = new ConcurrentHashMap<>();

    private DruidProperties druidProperties;

    public DruidJdbcPoolImpl(DruidProperties druidProperties) {
        if (druidProperties == null) {
            this.druidProperties = DruidProperties.getDefault();
        } else {
            this.druidProperties = druidProperties;
        }
    }

    public DruidDataSource getJdbcConnectionPool(JdbcDataSourceDefinition definition) {
        if (map.containsKey(definition.getCode())) {
            return map.get(definition.getCode());
        } else {
            try {
                if (!map.containsKey(definition.getCode())) {
                    DruidDataSource pool = createJdbcConnectionPool(definition);
                    map.put(definition.getCode(), pool);
                }
                return map.get(definition.getCode());
            } finally {
            }
        }
    }

    private DruidDataSource createJdbcConnectionPool(JdbcDataSourceDefinition definition) {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(definition.getDbUrl());
        datasource.setUsername(definition.getDbUsername());
        datasource.setPassword(definition.getDbPassword());
        datasource.setDriverClassName(definition.getDbDriver());
        //configuration
        datasource.setInitialSize(druidProperties.getInitialSize());
        datasource.setMinIdle(druidProperties.getMinIdle());
        datasource.setMaxActive(druidProperties.getMaxActive());
        datasource.setMaxWait(druidProperties.getMaxWait());

        datasource.setRemoveAbandoned(false);
        datasource.setRemoveAbandonedTimeout(1800);
        datasource.setLogAbandoned(false);

        datasource.setTimeBetweenEvictionRunsMillis(druidProperties.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(druidProperties.getMinEvictableIdleTimeMillis());

        return datasource;
    }


    /**
     * 删除数据库连接池
     *
     * @param code
     */
    @Override
    public void removeJdbcConnectionPool(String code) {
        try {
            DruidDataSource pool = map.get(code);
            if (pool != null) {
                map.remove(code);
            }
        } catch (Exception e) {
            throw new ReportException(e);
        }
    }

    /**
     * 获取连接
     *
     * @param dataSource
     * @return
     * @throws SQLException
     */
    @Override
    public Connection getPooledConnection(JdbcDataSourceDefinition dataSource) throws SQLException {
        DruidDataSource pool = getJdbcConnectionPool(dataSource);
        return pool.getConnection();
    }

    /**
     * 测试数据库连接  获取一个连接
     *
     * @param dataSource
     * @return
     * @throws ClassNotFoundException driverName不正确
     * @throws SQLException
     */
    @Override
    public void testConnection(JdbcDataSourceDefinition dataSource) throws SQLException {
        Connection conn = null;
        try {
            Class.forName(dataSource.getDbDriver());
            conn = DriverManager.getConnection(dataSource.getDbUrl(), dataSource.getDbUsername(), dataSource.getDbPassword());
        } catch (ClassNotFoundException ex) {
            throw new SQLException(ex);
        } catch (SQLException ex) {
            throw ex;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    throw ex;
                }
            }
        }

    }
}